package edu.yzu.testspringboot002.convert;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;

import edu.yzu.testspringboot002.common.constant.CacheColumnFamilyConstants;
import edu.yzu.testspringboot002.po.Dept;
import edu.yzu.testspringboot002.po.Doctor;
import edu.yzu.testspringboot002.po.dto.DoctorCallInfoDTO;
import edu.yzu.testspringboot002.po.vo.DoctorDetailVO;
import edu.yzu.testspringboot002.po.vo.DoctorInfoVO;
import edu.yzu.testspringboot002.po.vo.PatinetVO;
import edu.yzu.testspringboot002.utils.RocksDBUtil;
import org.mapstruct.Mapper;
import org.mapstruct.NullValuePropertyMappingStrategy;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static edu.yzu.testspringboot002.common.constant.CacheColumnFamilyConstants.DEPT_INFO;




@Mapper(
        componentModel = "spring",
        nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE
)
public abstract class DoctorsConvertMapper {

    public List<Doctor> toExchangeDeptCode(List<Doctor> doctorsList) {
        if (CollUtil.isNotEmpty(doctorsList)) {
            doctorsList.forEach(doctors -> {
                Dept dept = RocksDBUtil.get(DEPT_INFO, doctors.getDeptCode(), Dept.class);
                if (dept != null) {
                    Long pid = dept.getPid();
                    if (ObjectUtil.isNotNull(pid)) {
                        Dept p = RocksDBUtil.get(DEPT_INFO, String.valueOf(pid), Dept.class);
                        if (p != null) {
                            doctors.setDeptName(p.getName() + "/" + dept.getName());
                        } else {
                            doctors.setDeptName(dept.getName());
                        }
                    } else {
                        doctors.setDeptName(dept.getName());
                    }

                }
            });
        }
        return doctorsList;
    }

    public List<DoctorInfoVO> toExchangeDoctorInfoVO(Map<String, List<Doctor>> listMap) {
        List<DoctorInfoVO> doctorInfoVOList = new ArrayList<>();
        listMap.forEach((key, val) -> {

            Dept dept = RocksDBUtil.get(DEPT_INFO, key, Dept.class);
            if (dept != null) {
                DoctorInfoVO doctorInfoVO = new DoctorInfoVO(dept.getName(), key, val);
                doctorInfoVOList.add(doctorInfoVO);
            }
        });
        return doctorInfoVOList;
    }

    public Doctor toExchangeDeptCodeOne(Doctor doctors) {
        Dept dept = RocksDBUtil.get(DEPT_INFO, doctors.getDeptCode(), Dept.class);
        if (dept != null) {
            Long pid = dept.getPid();
            if (ObjectUtil.isNotNull(pid)) {
                Dept p = RocksDBUtil.get(DEPT_INFO, String.valueOf(pid), Dept.class);
                if (p != null) {
                    doctors.setDeptName(p.getName() + "/" + dept.getName());
                } else {
                    doctors.setDeptName(dept.getName());
                }
            } else {
                doctors.setDeptName(dept.getName());
            }

        }
        return doctors;
    }

    public abstract DoctorDetailVO toDoctorDetailVO(Doctor doctors);

    public List<PatinetVO> toPatinetVOList(List<Doctor> doctorsList) {
        List<PatinetVO> list = new ArrayList<>();
        for (Doctor doctors : doctorsList) {
            list.add(toPatientVO(doctors));
        }
        return list;
    }


    public abstract PatinetVO toPatientVO(Doctor doctors);


    public List<DoctorCallInfoDTO> toDoctorCallInfoDTOList(List<Doctor> doctorsList) {
        List<DoctorCallInfoDTO> list = new ArrayList<>();
        for (Doctor doctors: doctorsList){
            DoctorCallInfoDTO doctorCallInfoDTO = new DoctorCallInfoDTO();
            doctorCallInfoDTO.setNick(doctors.getUserName());
            doctorCallInfoDTO.setAvatar(doctors.getImgUrl());
            doctorCallInfoDTO.setUserID(doctors.getUserName());
            doctorCallInfoDTO.setChecked(true);
            list.add(doctorCallInfoDTO);
        }
        return list;
    }
}

